package com.niiwoo.civet.trade.service.local.intelligenceInvest;

import com.alibaba.dubbo.config.annotation.Reference;
import com.niiwoo.activity.stage.service.prize.ActivityPrizeDubboService;
import com.niiwoo.civet.account.service.AccountDubboService;
import com.niiwoo.civet.trade.dao.entity.NewbieProjectConfig;
import com.niiwoo.civet.trade.dao.entity.ProjectInvestOrder;
import com.niiwoo.civet.trade.dao.mapper.NewbieProjectConfigMapperExt;
import com.niiwoo.civet.trade.dao.mapper.ProjectInvestOrderMapperExt;
import com.niiwoo.civet.trade.dto.response.intelligenceInvest.NoviceIntelligenceInvestInfoResponseDTO;
import com.niiwoo.civet.trade.utils.RepaymentUtil;
import com.niiwoo.civet.user.dto.response.UserBasicInfoResponseDTO;
import com.niiwoo.civet.user.service.OrganizationDubboService;
import com.niiwoo.civet.user.service.UserBasicInfoDubboService;
import com.niiwoo.civet.user.service.UserIdentityDubboService;
import com.niiwoo.tripod.provider.exception.BizException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;

@Service
@Slf4j
public class NoviceIntelligenceInvestService {

	@Reference(version = "1.0.0")
	private UserBasicInfoDubboService userBasicInfoDubboService;
	@Reference(version = "1.0.0")
	private AccountDubboService accountDubboService;
	@Reference(version = "1.0.0")
	private ActivityPrizeDubboService activityPrizeDubboService;
	@Reference(version = "1.0.0")
	private OrganizationDubboService organizationDubboService;

	@Autowired
	private NewbieProjectConfigMapperExt newbieProjectConfigMapperExt;
	@Autowired
	private IntelligenceInvestService intelligenceInvestService;
	@Autowired
	private ProjectInvestOrderMapperExt projectInvestOrderMapperExt;

	@Reference(version = "1.0.0")
	private UserIdentityDubboService userIdentityDubboService;

	public NoviceIntelligenceInvestInfoResponseDTO getNoviceBasicInfo(String userId) {
		NoviceIntelligenceInvestInfoResponseDTO responseDTO = new NoviceIntelligenceInvestInfoResponseDTO();
		NewbieProjectConfig config = newbieProjectConfigMapperExt.selectNewbieProjectConfig(null);
		if (config == null) {
			throw new BizException("TRD10032");
		}
		UserBasicInfoResponseDTO userBasicInfoDTO = userBasicInfoDubboService.selectUserBaseInfo(userId);
		if(userBasicInfoDTO == null){
			throw new BizException("USR10028");
		}
		boolean isNovice;
		//1.在配置时间以后注册或者当天注册  2.在配置时间以前没有投资的用户  都算新手(享受配置额度)
		Integer registerDay = RepaymentUtil.getOverdueDay(userBasicInfoDTO.getCreateTime(), config.getRoleDefinitionDate());

		ProjectInvestOrder projectInvestOrder = projectInvestOrderMapperExt.selectFirstInvestByUser(userId);
		if (projectInvestOrder == null) {
			isNovice = true;
		} else {
			Integer fristInvestDay = RepaymentUtil.getOverdueDay(config.getRoleDefinitionDate(), projectInvestOrder.getCreateTime());
			isNovice = registerDay <= 0 || fristInvestDay >= 0;
		}

		BigDecimal hadInvestMoneyNovice = projectInvestOrderMapperExt.selectIsNoviceMoneyTotal(userId, config.getChannelCode());
		if (hadInvestMoneyNovice == null) {
			hadInvestMoneyNovice = BigDecimal.ZERO;
		}
		BigDecimal hadInvestPackage = intelligenceInvestService.selectInvestMoneyTotalForMaster(userId);
		if (hadInvestPackage == null) {
			hadInvestPackage = BigDecimal.ZERO;
		}
		BigDecimal totalHadInvestMoney = hadInvestMoneyNovice.add(hadInvestPackage);
		BigDecimal investNorm = new BigDecimal(Integer.toString(config.getInvestmentNorm()));
		responseDTO.setDays(isNovice ? 1 : 0);
		responseDTO.setInvestmentNorm(investNorm);
		responseDTO.setRoleDefinitionDays(config.getRoleDefinition());
		responseDTO.setNoviceSurplusDays(isNovice ? 1 : 0);
		responseDTO.setNoviceSurplusInvestMoney(
				isNovice ? (totalHadInvestMoney.compareTo(investNorm) >= 0 ? BigDecimal.ZERO : investNorm.subtract(totalHadInvestMoney)) : BigDecimal.ZERO);
		return responseDTO;
	}



}
